package com.jxtti.service;

import org.apache.log4j.Logger;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.jxtti.dao.AdvisoryMsgDao;
import com.jxtti.entity.AdvisoryMsg;

/**
 * 
 * <br>
 * <b>功能：</b>AdvisoryMsgService<br>
 * <b>作者：lwb</b><br>
 * <b>日期：</b> Nov 26, 2015 <br>
 * <b>版权所有：<b>版权所有(C) 2015，<br>
 */
@Service("advisoryMsgService")
@Transactional(rollbackFor = { RuntimeException.class, Exception.class })
public class AdvisoryMsgService {

	private final static Logger log= Logger.getLogger(AdvisoryMsgService.class);
	

	

	@Autowired
    private AdvisoryMsgDao advisoryMsgDao;


	/**
	 * 获取信息数据列表--后台
	 * 
	 * @param data
	 * @return
	 */	
	public PageInfo<AdvisoryMsg> list(Map<String, Object> data) {
		// 第几页
		int pageNum = Integer.parseInt((data.get("page") == null || data.get("page").equals("0")) ? "1" : data.get("page").toString());
		// 每页显示条数 
		int pageSize = Integer.parseInt((data.get("rows") == null || data.get("rows").equals("0")) ? "10" : data.get("rows").toString());

		PageHelper.startPage(pageNum, pageSize);
		PageInfo<AdvisoryMsg> pages = new PageInfo<AdvisoryMsg>(advisoryMsgDao.queryByList(data));
		return pages;
	}
	
	public Map<String, Object> listDesk(Map<String, Object> data,  AdvisoryMsg advisoryMsg){
		
		int pageNum = Integer.parseInt((data.get("currentPage") == null || data.get("currentPage").equals("0")) ? "1" : data.get("currentPage").toString());

		int pageSize = Integer.parseInt((data.get("pageSize") == null || data.get("pageSize").equals("0")) ? "10" : data.get("pageSize").toString());
		PageHelper.startPage(pageNum, pageSize);
		PageInfo<AdvisoryMsg> pages = new PageInfo<AdvisoryMsg>( advisoryMsgDao.queryByList(data));
		data.put("page", pages);
		return data;
	}
	
	
	
	// 前台分页
	public  PageInfo<AdvisoryMsg> listPage(Map<String, Object> data){
		//第几页
		int pageNum = Integer.parseInt((data.get("currPageNum") == null || data.get("currPageNum").equals("0")) ? "1" : data.get("currPageNum").toString());
		// 每页显示条数 
		int pageSize = Integer.parseInt((data.get("pageRowSize") == null || data.get("pageRowSize").equals("0")) ? "10" : data.get("pageRowSize").toString());
		
		PageHelper.startPage(pageNum, pageSize);			
		PageInfo<AdvisoryMsg> pages = new PageInfo<AdvisoryMsg>(advisoryMsgDao.queryForIndex(data));		
		return pages;
	}
	
	
	public List<AdvisoryMsg>  queryForIndex(Map<String, Object> data){
		return  advisoryMsgDao.queryForIndex(data);
	}
	
	
	/**
	 * 查询所有数据。 
	 * @param data
	 * @return
	 */
	public List<AdvisoryMsg>  queryAll(Map<String, Object> data)
	{
		return  advisoryMsgDao.queryByList(data);
	}
	
	
	
	/**
	 * 添加数据.
	 * 
	 * @param data
	 * @return
	 */
	public int add(AdvisoryMsg advisoryMsg)
	{
		int insertCount = advisoryMsgDao.add(advisoryMsg);
		return insertCount;
	}
	
    //信访系统编号流水号加1后返回  
    public String getPetitionNo(){  
    	String latelyNum="";
		// 查找当前信访件编号	360700  2015  X  0000011
    	String PetitionRegionCode="360700";
    	//ad_work_unit 用工作单位字段 存放 信访件编号
    	AdvisoryMsg  latelyPetition =advisoryMsgDao.queryLatelyPetitionNo();
		
		if(latelyPetition!=null){
			String petitionNo=latelyPetition.getAd_work_unit();				
			String liuShuiHao=petitionNo.substring(petitionNo.length()-7,petitionNo.length());
			
			Integer intHao = Integer.parseInt(liuShuiHao);  
		    	intHao++;  
		    String strHao = intHao.toString();  
		    while (strHao.length() < liuShuiHao.length())  
		           strHao = "0" + strHao; 
		        
			latelyNum=strHao;				
		}else{
			latelyNum="0000001";
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
	    Date date = new Date();
	    String formatDate = sdf.format(date);      
	   String petitionTypeName="W";//网络舆情			
		String petitionNo=PetitionRegionCode+formatDate+petitionTypeName+latelyNum; 	       
        return petitionNo;  
    }
	
	//信访系统
	public int addPetition(AdvisoryMsg advisoryMsg){
		advisoryMsg.setAd_work_unit(getPetitionNo());
		int insertCount = advisoryMsgDao.addPetition(advisoryMsg);
		return insertCount;
	}
	

	/**
	 * 修改数据.
	 * 
	 * @param data
	 * @return
	 */
	public int update(AdvisoryMsg advisoryMsg){
		int updateCount = advisoryMsgDao.update(advisoryMsg);
		return updateCount;
	}
	

	/**
	 * 删除数据.
	 * 
	 * @param data
	 * @return
	 */
	public int delete(AdvisoryMsg advisoryMsg)
	{
		int deleteCount = advisoryMsgDao
				.delete(advisoryMsg);
		
		return deleteCount;
	}
	
	
	public AdvisoryMsg queryAdvisoryMsgById(AdvisoryMsg advisoryMsg){
		advisoryMsg = advisoryMsgDao.queryAdvisoryMsgById(advisoryMsg);
		return advisoryMsg;
	}
	
	// 查询总数
	public AdvisoryMsg queryStatistics(AdvisoryMsg advisoryMsg){
		advisoryMsg = advisoryMsgDao.queryStatistics(advisoryMsg);
		return advisoryMsg;
	}
	public AdvisoryMsg queryChart1Statistics(AdvisoryMsg advisoryMsg){
		advisoryMsg = advisoryMsgDao.queryChart1Statistics(advisoryMsg);
		return advisoryMsg;
	}
	public AdvisoryMsg queryChart2Statistics(AdvisoryMsg advisoryMsg){
		advisoryMsg = advisoryMsgDao.queryChart2Statistics(advisoryMsg);
		return advisoryMsg;
	}
	public AdvisoryMsg queryChart3Statistics(AdvisoryMsg advisoryMsg){
		advisoryMsg = advisoryMsgDao.queryChart3Statistics(advisoryMsg);
		return advisoryMsg;
	}

}
